package com.wwmz.proxy.pattern.dynamicproxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by wumz on 2020/9/21 10:18
 */
public class JdkMeipo implements InvocationHandler {

    private IPerson target;

    public IPerson getInstance(IPerson target){
        this.target = target;
        Class<? extends IPerson> aClass = target.getClass();
        return (IPerson)Proxy.newProxyInstance(aClass.getClassLoader(), aClass.getInterfaces(), this::invoke);
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object invoke = method.invoke(this.target, args);
        after();
        return invoke;
    }

    private void before(){
        System.out.println("我已接收到你的请求，开始物色");
    }

    private void after(){
        System.out.println("双方同意，开始交往");
    }
}
